package com.agentframework.core.plan;

import com.agentframework.core.tools.ToolManager;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.ai.tool.ToolCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangjun
 **/
public class Task {

    @JsonProperty("task_id")
    private String taskId;

    @JsonProperty("dependent_task_ids")
    private List<String> dependentTaskIds;

    @JsonProperty("instruction")
    private String instruction;

    @JsonProperty("task_tools")
    private List<String> taskTools;

    private String result;

    private Boolean isSuccess = false;

    private Boolean isFinished = false;

    public Task() {

    }

    public Task(String taskId, List<String> dependentTaskIds, String instruction, List<String> taskTools) {
        this.taskId = taskId;
        this.dependentTaskIds = dependentTaskIds;
        this.instruction = instruction;
        this.taskTools = taskTools;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public List<String> getDependentTaskIds() {
        return dependentTaskIds;
    }

    public void setDependentTaskIds(List<String> dependentTaskIds) {
        this.dependentTaskIds = dependentTaskIds;
    }

    public String getInstruction() {
        return instruction;
    }

    public void setInstruction(String instruction) {
        this.instruction = instruction;
    }

    public List<String> getTaskTools() {
        return taskTools;
    }

    public void setTaskTools(List<String> taskTools) {
        this.taskTools = taskTools;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public Boolean getSuccess() {
        return isSuccess;
    }

    public void setSuccess(Boolean success) {
        isSuccess = success;
    }

    public Boolean getFinished() {
        return isFinished;
    }

    public void setFinished(Boolean finished) {
        isFinished = finished;
    }

    public void reset() {
        this.result = null;
        this.isSuccess = false;
        this.isFinished = false;
    }

    public void updateTaskResult(TaskResult taskResult) {
        this.result = taskResult.result();
        this.isSuccess = taskResult.isSuccess();
    }

    public List<ToolCallback> getToolCallbacks() {
        List<ToolCallback> toolCallbacks = new ArrayList<>();
        for (String taskTool : getTaskTools()) {
            toolCallbacks.add(ToolManager.getTool(taskTool.trim()));
        }
        return toolCallbacks;
    }

    @Override
    public String toString() {
        return "Task{" +
                "taskId='" + taskId + '\'' +
                ", dependentTaskIds=" + dependentTaskIds +
                ", instruction='" + instruction + '\'' +
                '}';
    }

}
